package com.timmy.test1.common;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.media.Image;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ResourceUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.timmy.test1.App;
import com.timmy.test1.DialogFactory;
import com.timmy.test1.JVkeyBean;
import com.timmy.test1.R;
import com.timmy.test1.util.CustomVKeyUtil;
import com.timmy.test1.view.CustomKeyView;
import com.timmy.test1.view.CustomRockerView;
import com.timmy.test1.view.CustomRoundView;
import com.timmy.test1.view.ImageSeekBar;
import com.timmy.test1.view.JVKeyView;
import com.timmy.test1.view.KeyView;
import com.timmy.test1.view.RockerView;
import com.timmy.test1.view.RoundView;
import com.timmy.test1.view.SupportLineView;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by timmy on 2020/5/7.
 */
public class JiActivityVKeyHelper implements JVKeyView.JVKeyMouseListener, JVKeyView.RockerViewListener, View.OnClickListener, CustomKeyView.OnCusKeyViewClickListener, View.OnTouchListener, SupportLineView.OnSupportLineViewClickListener {

    public static int mActivityHeight;
    public static int mActivityWidth;

    JVKeyView mCurrentJVKeyView;

    private View     mView;
    private Activity mActivity;
    private Activity mJiActivity;

    private ImageSeekBar ji_alpha;


    public JiActivityVKeyHelper(Activity paramActivity, View paramView, int paramInt1, int paramInt2) {
        this.mView = paramView;
        this.mActivity = paramActivity;
        mActivityWidth = paramInt1;
        mActivityHeight = paramInt2;

        init();
    }

    private void init() {
        initView();
        initAlpha();
        initClickListener();
    }

    private void initAlpha() {
        JVKeyView.viewAlpha = 0.8f;
        ji_alpha.setProgress((int) ((0.8f - 0.1F) * 100.0F / 0.9F));
        ji_alpha.setOnProgressChangeListener(new ImageSeekBar.OnProgressChangedListener() {
            @Override
            public void onProgressChanged(int param1Int, float param1Float) {

            }
        });
    }

    private FrameLayout  fl_custom_vkey_container;
    private FrameLayout  fl_custom_vkey_choose_container;
    private LinearLayout ll_vkey_choose_mouse;
    private LinearLayout ll_vkey_choose_shake;
    private FrameLayout  fl_vkey_preview_container;
    private View         ll_vkey_choose;
    private View         fl_vkey_menu;
    public  FrameLayout  fl_vkey_container;
    public  View         ll_vkey_review;
    public  View         fl_vkey_custom;
    private View         layout_preview;
    private View         tv_vkey_review_preview;
    private View         tv_exit_preview;
    private TextView     btn_edit_custom;
    private TextView     tv_vkey_review_edit;
    private LinearLayout ll_vkey_cons;
    private LinearLayout ll_vkey_config_cons;
    private View         ll_save_config;

    private ImageView v_custom_vkey_mouse;
    private ImageView v_custom_vkey_key;
    private ImageView v_custom_vkey_shake;
    private ImageView v_custom_vkey_support;
    private ImageView v_custom_vkey_save;

    private LinearLayout ll_vkey_choose_support;
    private View         iv_custom_vkey_choose_container_close;
    private View         ll_custom_vkey_add_mouse_left;
    private View         ll_custom_vkey_add_mouse_right;
    private View         ll_custom_vkey_add_mouse_middle;
    private View         ll_custom_vkey_add_scroller_up;
    private View         ll_custom_vkey_add_scroller_down;

    private View ll_custom_vkey_add_shake_wasd;
    private View ll_custom_vkey_add_shake_arrow;
    private View ll_custom_vkey_add_fire;

    // 触屏左右 吸附线
    private View ll_custom_vkey_touch_lr_sw;
    private View ll_custom_vkey_mouse_lr_sw;
    private View ll_custom_vkey_add_support_horizontal;
    private View ll_custom_vkey_add_support_vertical;
    private View ll_custom_vkey_lock_screen_slide;
    private View btn_new_custom;

    private void initView() {
        ji_alpha = mView.findViewById(R.id.ji_alpha);
        fl_custom_vkey_container = mView.findViewById(R.id.fl_custom_vkey_container);
        fl_custom_vkey_choose_container = mView.findViewById(R.id.fl_custom_vkey_choose_container);
        ll_vkey_choose_mouse = mView.findViewById(R.id.ll_vkey_choose_mouse);
        ll_vkey_choose_shake = mView.findViewById(R.id.ll_vkey_choose_shake);
        fl_vkey_preview_container = mView.findViewById(R.id.fl_vkey_preview_container);
        ll_vkey_choose = mView.findViewById(R.id.ll_vkey_choose);
        fl_vkey_menu = mView.findViewById(R.id.fl_vkey_menu);
        fl_vkey_container = mView.findViewById(R.id.fl_vkey_container);

        this.ll_vkey_review = this.mView.findViewById(R.id.ll_vkey_review);
        this.fl_vkey_custom = this.mView.findViewById(R.id.fl_vkey_custom);
        this.layout_preview = this.mView.findViewById(R.id.layout_preview);
        this.tv_exit_preview = this.mView.findViewById(R.id.tv_exit_preview);
        this.tv_vkey_review_edit = this.mView.findViewById(R.id.tv_vkey_review_edit);

        tv_vkey_review_preview = this.mView.findViewById(R.id.tv_vkey_review_preview);
        btn_edit_custom = this.mView.findViewById(R.id.btn_edit_custom);
        ll_vkey_cons = this.mView.findViewById(R.id.ll_vkey_cons);
        ll_vkey_config_cons = this.mView.findViewById(R.id.ll_vkey_config_cons);
        ll_save_config = this.mView.findViewById(R.id.ll_save_config);

        v_custom_vkey_mouse = this.mView.findViewById(R.id.v_custom_vkey_mouse);
        v_custom_vkey_key = this.mView.findViewById(R.id.v_custom_vkey_key);
        v_custom_vkey_shake = this.mView.findViewById(R.id.v_custom_vkey_shake);
        v_custom_vkey_support = this.mView.findViewById(R.id.v_custom_vkey_support);
        v_custom_vkey_save = this.mView.findViewById(R.id.v_custom_vkey_save);

        ll_vkey_choose_support = this.mView.findViewById(R.id.ll_vkey_choose_support);
        iv_custom_vkey_choose_container_close = this.mView.findViewById(R.id.iv_custom_vkey_choose_container_close);

        ll_custom_vkey_add_mouse_left = this.mView.findViewById(R.id.ll_custom_vkey_add_mouse_left);
        ll_custom_vkey_add_mouse_right = this.mView.findViewById(R.id.ll_custom_vkey_add_mouse_right);

        ll_custom_vkey_add_mouse_middle = this.mView.findViewById(R.id.ll_custom_vkey_add_mouse_middle);
        ll_custom_vkey_add_scroller_up = this.mView.findViewById(R.id.ll_custom_vkey_add_scroller_up);
        ll_custom_vkey_add_scroller_down = this.mView.findViewById(R.id.ll_custom_vkey_add_scroller_down);


        ll_custom_vkey_add_shake_wasd = this.mView.findViewById(R.id.ll_custom_vkey_add_shake_wasd);
        ll_custom_vkey_add_shake_arrow = this.mView.findViewById(R.id.ll_custom_vkey_add_shake_arrow);
        ll_custom_vkey_add_fire = this.mView.findViewById(R.id.ll_custom_vkey_add_fire);

        ll_custom_vkey_touch_lr_sw = this.mView.findViewById(R.id.ll_custom_vkey_touch_lr_sw);
        ll_custom_vkey_mouse_lr_sw = this.mView.findViewById(R.id.ll_custom_vkey_mouse_lr_sw);
        ll_custom_vkey_add_support_horizontal = this.mView.findViewById(R.id.ll_custom_vkey_add_support_horizontal);
        ll_custom_vkey_add_support_vertical = this.mView.findViewById(R.id.ll_custom_vkey_add_support_vertical);
        ll_custom_vkey_lock_screen_slide = this.mView.findViewById(R.id.ll_custom_vkey_lock_screen_slide);
        btn_new_custom = this.mView.findViewById(R.id.btn_new_custom);

        initKeySetView();
    }

    private TextView     tv_once;
    private TextView     tv_continuity;
    private TextView     tv_long;
    private TextView     tv_size;
    private TextView     tv_frequency;
    private ImageView    iv_btn_left_size;
    private ImageView    iv_btn_right_size;
    private ImageView    iv_btn_left_frequency;
    private ImageView    iv_btn_right_frequency;
    private LinearLayout layout_frequency;
    private View         v_custom_vkey_cancel;
    private View         ll_dialog_cancel_add;

    private void initKeySetView() {
        this.dialog = this.mView.findViewById(R.id.dialog_cuskey);
        this.tv_long = (TextView) mView.findViewById(R.id.tv_long);
        this.tv_once = this.dialog.findViewById(R.id.tv_once);
        this.tv_continuity = (TextView) this.dialog.findViewById(R.id.tv_continuity);
        this.iv_btn_left_size = (ImageView) this.dialog.findViewById(R.id.iv_btn_left_size);
        this.iv_btn_right_size = (ImageView) this.dialog.findViewById(R.id.iv_btn_right_size);
        this.tv_size = (TextView) this.dialog.findViewById(R.id.tv_size);
        this.iv_btn_left_frequency = (ImageView) this.dialog.findViewById(R.id.iv_btn_left_frequency);
        this.iv_btn_right_frequency = (ImageView) this.dialog.findViewById(R.id.iv_btn_right_frequency);
        this.tv_frequency = (TextView) this.dialog.findViewById(R.id.tv_frequency);
        this.layout_frequency = (LinearLayout) this.dialog.findViewById(R.id.layout_frequency);
        ((LinearLayout) this.dialog.findViewById(R.id.layout_frequency)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View param1View) {
                LogUtils.i("屏蔽其他区域点击");
            }
        });

        v_custom_vkey_cancel = this.mView.findViewById(R.id.v_custom_vkey_cancel);
        ll_dialog_cancel_add = this.mView.findViewById(R.id.ll_dialog_cancel_add);


    }

    private void initClickListener() {
        this.tv_vkey_review_preview.setOnClickListener(this);
        this.tv_exit_preview.setOnClickListener(this);
        this.btn_edit_custom.setOnClickListener(this);
        this.tv_vkey_review_edit.setOnClickListener(this);
        this.v_custom_vkey_cancel.setOnClickListener(this);

        this.v_custom_vkey_mouse.setOnClickListener(this);
        this.v_custom_vkey_key.setOnClickListener(this);
        this.v_custom_vkey_shake.setOnClickListener(this);
        this.v_custom_vkey_support.setOnClickListener(this);
        this.v_custom_vkey_save.setOnClickListener(this);

        this.iv_custom_vkey_choose_container_close.setOnClickListener(this);
        this.ll_custom_vkey_add_mouse_left.setOnClickListener(this);
        this.ll_custom_vkey_add_mouse_right.setOnClickListener(this);

        this.ll_custom_vkey_add_mouse_middle.setOnClickListener(this);
        this.ll_custom_vkey_add_scroller_up.setOnClickListener(this);
        this.ll_custom_vkey_add_scroller_down.setOnClickListener(this);

        this.ll_custom_vkey_add_shake_wasd.setOnClickListener(this);
        this.ll_custom_vkey_add_shake_arrow.setOnClickListener(this);
        this.ll_custom_vkey_add_fire.setOnClickListener(this);

        this.ll_custom_vkey_touch_lr_sw.setOnClickListener(this);
        this.ll_custom_vkey_mouse_lr_sw.setOnClickListener(this);
        this.ll_custom_vkey_add_support_horizontal.setOnClickListener(this);
        this.ll_custom_vkey_add_support_vertical.setOnClickListener(this);
        this.ll_custom_vkey_lock_screen_slide.setOnClickListener(this);
        this.btn_new_custom.setOnClickListener(this);
    }

    private void initData() {
        updateVkeyConfig();
    }

    private void updateVkeyConfig() {


    }

    private void useCustomKeyView(List<JVkeyBean> paramList) {
        this.fl_custom_vkey_container.setVisibility(View.GONE);
        this.fl_custom_vkey_choose_container.setVisibility(View.GONE);
        this.ll_vkey_choose_mouse.setVisibility(View.GONE);
        this.ll_vkey_choose_shake.setVisibility(View.GONE);

        // 隐藏软键盘

        // 显示按键
        JVKeyView jvKeyView = new JVKeyView(mActivity);
        jvKeyView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        ViewGroup viewGroup = (ViewGroup) LayoutInflater.from(mActivity).inflate(R.layout.layout_vkey_custom, null);

        for (JVkeyBean jVkeyBean : paramList) {
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(150, 150);

            layoutParams.leftMargin = Math.round((float) (jVkeyBean.getLeftMargin() / 100.0D) * mActivityWidth);
            layoutParams.topMargin = Math.round((float) (jVkeyBean.getTopMargin() / 100.0D) * mActivityHeight);


            layoutParams.width = Math.round((float) (jVkeyBean.getWidth() / 100.0D) * mActivityWidth);
            layoutParams.height = Math.round((float) (jVkeyBean.getHeight() / 100.0D) * mActivityWidth);

            if (jVkeyBean.getKeyType() == 1) {
                RoundView roundView = new RoundView(this.mActivity);
                roundView.setScanCode(jVkeyBean.getScanCode());
                roundView.setLongPress(jVkeyBean.isLongPress());
                roundView.setFling(jVkeyBean.isFling());
                roundView.setTextColor(ContextCompat.getColor(mActivity, R.color.white));
                roundView.setTypeface(Typeface.DEFAULT_BOLD);
                roundView.setGravity(Gravity.CENTER);
                roundView.setContinuous(jVkeyBean.isContinuous());
                roundView.setContinuousFrequency(jVkeyBean.getContinuousFrequency());

                if (jVkeyBean.isRound() || roundView.getScanCode() == 57) {
                    roundView.setBackground(ContextCompat.getDrawable(mActivity, CustomVKeyUtil.getVKeyBackground(roundView.getScanCode(), roundView.isFling())));
                    roundView.setNormalBackground(CustomVKeyUtil.getVKeyBackground(roundView.getScanCode(), roundView.isFling()));
                    roundView.setPressBackground(CustomVKeyUtil.getVKeyPressBackground(roundView.getScanCode(), roundView.isFling()));
                } else {
                    roundView.setBackground(ContextCompat.getDrawable(mActivity, R.mipmap.key_tab_normal));
                    roundView.setNormalBackground(R.mipmap.key_tab_normal);
                    roundView.setPressBackground(R.mipmap.key_tab_press);
                }

                roundView.setRound(jVkeyBean.isRound());
                roundView.setLayoutParams(layoutParams);
                viewGroup.addView(roundView);
                continue;
            }

            if (jVkeyBean.getKeyType() == 2) {
                RockerView rockerView = (RockerView) this.mActivity.getLayoutInflater().inflate(R.layout.v_rocker, null);
                rockerView.setRockerType(jVkeyBean.getRockType());
                rockerView.setLayoutParams(layoutParams);
                rockerView.setRockerRadiusBySize(jVkeyBean.getSize());

                if (jVkeyBean.getRockType() == 0) {
                    rockerView.setRockBackOut(((BitmapDrawable) this.mActivity.getResources().getDrawable(R.mipmap.rock_wasd_normal)).getBitmap());
                    rockerView.setRockBackIn(((BitmapDrawable) this.mActivity.getResources().getDrawable(R.mipmap.rock_bg_in)).getBitmap());
                    rockerView.setBackground(this.mActivity.getResources().getDrawable(R.mipmap.rock_wasd_press));
                } else {
                    rockerView.setRockBackOut(((BitmapDrawable) this.mActivity.getResources().getDrawable(R.mipmap.rock_bg_out_normal)).getBitmap());
                    rockerView.setRockBackIn(((BitmapDrawable) this.mActivity.getResources().getDrawable(R.mipmap.rock_bg_in)).getBitmap());
                    rockerView.setBackground(this.mActivity.getResources().getDrawable(R.mipmap.rock_bg_out_press));
                }
                viewGroup.addView(rockerView);
            }
        }
    }

    private int mCurrentOpMode;
    private int mCurrentConfig;

    public void onServerReviewVKeyDataSuccess(int paramInt, final String keypos_data) {

        List list = (List) (new Gson()).fromJson(keypos_data, (new TypeToken<List<JVkeyBean>>() {
        }).getType());

        if (mCurrentOpMode == 4) {
            useCustomKeyView(list);
        } else if (mCurrentOpMode == 5) {
            JiActivityVKeyHelper.this.fl_vkey_preview_container.setVisibility(View.VISIBLE);
            JiActivityVKeyHelper.this.ll_vkey_choose.setVisibility(View.GONE);
            JiActivityVKeyHelper.this.fl_vkey_menu.setVisibility(View.GONE);
            JiActivityVKeyHelper.this.clearKey();
            JiActivityVKeyHelper.this.fl_custom_vkey_container.removeAllViews();
            JiActivityVKeyHelper.this.fl_custom_vkey_container.setVisibility(View.GONE);
            JiActivityVKeyHelper.this.showPreviewVkey(list);
        }
    }

    private void showPreviewVkey(List<JVkeyBean> paramList) {
        this.fl_vkey_preview_container.setVisibility(View.VISIBLE);

        this.ll_vkey_choose.setVisibility(View.GONE);
        this.fl_vkey_menu.setVisibility(View.GONE);

        clearKey();

        if (mCurrentOpMode == 7) {
            this.ll_vkey_review.setVisibility(View.GONE);
        }

        this.fl_vkey_custom.setVisibility(View.INVISIBLE);
        this.fl_custom_vkey_container.removeAllViews();
        this.fl_custom_vkey_container.setVisibility(View.INVISIBLE);

        JVKeyView jVKeyView = new JVKeyView(this.mActivity);
        jVKeyView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        ViewGroup viewGroup = (ViewGroup) LayoutInflater.from(mActivity).inflate(R.layout.layout_vkey_custom, null);

        for (JVkeyBean jVkeyBean : paramList) {
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(150, 150);
            layoutParams.leftMargin = (int) (jVkeyBean.getLeftMargin() / 100.0D * mActivityWidth);
            layoutParams.topMargin = (int) (jVkeyBean.getTopMargin() / 100.0D * mActivityHeight);
            layoutParams.width = (int) (jVkeyBean.getWidth() / 100.0D * mActivityWidth);
            layoutParams.height = (int) (jVkeyBean.getHeight() / 100.0D * mActivityWidth);

            switch (jVkeyBean.getKeyType()) {
                case 1:
                    RoundView roundView = new RoundView((Context) this.mActivity);
                    roundView.setScanCode(jVkeyBean.getScanCode());
                    roundView.setLongPress(jVkeyBean.isLongPress());
                    roundView.setFling(jVkeyBean.isFling());
                    roundView.setTextColor(-1);
                    roundView.setTypeface(Typeface.DEFAULT_BOLD);
                    roundView.setGravity(17);
                    if (jVkeyBean.isRound()) {
                        roundView.setBackground(this.mActivity.getResources().getDrawable(CustomVKeyUtil.getVKeyBackground(roundView.getScanCode(), roundView.isFling())));
                        roundView.setNormalBackground(CustomVKeyUtil.getVKeyBackground(roundView.getScanCode(), roundView.isFling()));
                        roundView.setPressBackground(CustomVKeyUtil.getVKeyPressBackground(roundView.getScanCode(), roundView.isFling()));
                    } else {
                        roundView.setBackground(ContextCompat.getDrawable(mActivity, R.mipmap.key_tab_normal));
                        roundView.setNormalBackground(R.mipmap.key_tab_normal);
                        roundView.setPressBackground(R.mipmap.key_tab_press);
                    }
                    roundView.setRound(jVkeyBean.isRound());
                    roundView.setLayoutParams(layoutParams);
                    viewGroup.addView(roundView);
                    break;
                case 2:
                    RockerView rockerView = (RockerView) this.mActivity.getLayoutInflater().inflate(R.layout.v_rocker, null);
                    rockerView.setRockerType(jVkeyBean.getRockType());
                    rockerView.setLayoutParams(layoutParams);
                    rockerView.setRockerRadiusBySize(jVkeyBean.getSize());
                    viewGroup.addView((View) rockerView);
                    break;
                default:
                    break;
            }
        }

        jVKeyView.setContentView(viewGroup);

        this.mCurrentJVKeyView = jVKeyView;

        jVKeyView.setJKeyListener(new JKeyListener() {
            @Override
            public void onJKeyDown(KeyView param1KeyView) {
                // 按键down
                Log.e(TAG, "按键 down KeyView " + param1KeyView.getText());
            }

            @Override
            public void onJKeyUp(KeyView param1KeyView) {
                // 按键up
                Log.e(TAG, "按键 up KeyView " + param1KeyView.getText());
            }
        });

        this.mCurrentJVKeyView.setJVKeyMouseListener(this);
        this.mCurrentJVKeyView.setRockerViewListener(this);

        this.fl_vkey_preview_container.addView((View) this.mCurrentJVKeyView);
        this.layout_preview.bringToFront();
    }

    private void clearKey() {
        JVKeyView jVKeyView = this.mCurrentJVKeyView;
        if (jVKeyView != null) {
            jVKeyView.releaseKey();
        }
        this.fl_vkey_container.removeAllViews();
        this.fl_vkey_container.setVisibility(View.GONE);
    }

    @Override
    public void onDispatchTouchEvent(MotionEvent param1MotionEvent) {
        if (JVKeyView.isCanTouchInVKey) {

        }

    }

    @Override
    public void onJVKeyLeftMouseDown() {

    }

    private static final String TAG = JiActivityVKeyHelper.class.getSimpleName();

    @Override
    public void onJVKeyLeftMouseUp() {
        Log.i(TAG, "onJVKeyLeftMouseUp");

    }

    @Override
    public void onJVKeyMiddleMouseDown() {
        Log.i(TAG, "onJVKeyMiddleMouseDown");

    }

    @Override
    public void onJVKeyMiddleMouseUp() {
        Log.i(TAG, "onJVKeyMiddleMouseUp");

    }

    private int mJVKeyMouseScrollV = 0;

    @Override
    public void onJVKeyMouseScroll(int paramInt, MotionEvent param1MotionEvent) {
        Log.i(TAG, "onJVKeyMouseScroll");

        this.mJVKeyMouseScrollV = paramInt;

    }

    @Override
    public void onJVKeyMove(int param1Int1, int param1Int2) {
        Log.i(TAG, "onJVKeyMove param1Int1: " + param1Int1 + " param1Int2: " + param1Int2);


    }

    @Override
    public void onJVKeyRightMouseDown() {
        Log.i(TAG, "onJVKeyRightMouseDown");

    }

    @Override
    public void onJVKeyRightMouseLongDown() {
        Log.i(TAG, "onJVKeyRightMouseLongDown");

    }

    @Override
    public void onJVKeyRightMouseUp() {
        Log.i(TAG, "onJVKeyRightMouseUp");

    }

    private Direction mCurrentRockerViewDirection;

    @Override
    public void onRockerViewDirection(Direction paramDirection, int rockerType) {

        Log.i("gzw", "onRockerViewDirection: " + paramDirection);

        byte b1;
        byte b2;
        byte b3;
        int number;
        if (rockerType == 1) {
            b3 = 103;
            number = 108;
            b1 = 105;
            b2 = 106;
        } else {
            b3 = 17;
            number = 31;
            b1 = 30;
            b2 = 32;
        }

        if (paramDirection == Direction.DIRECTION_UP) {
//            App.mJPresenter.mKeyPressed(EvdevTranslator.switchScanCode(b3));
        } else if (paramDirection == Direction.DIRECTION_DOWN) {
//            App.mJPresenter.mKeyPressed(EvdevTranslator.switchScanCode(paramInt));
        } else if (paramDirection == Direction.DIRECTION_LEFT) {
//            App.mJPresenter.mKeyPressed(EvdevTranslator.switchScanCode(b1));
        } else if (paramDirection == Direction.DIRECTION_RIGHT) {
//            App.mJPresenter.mKeyPressed(EvdevTranslator.switchScanCode(b2));
        } else if (paramDirection == Direction.DIRECTION_UP_LEFT) {
//            App.mJPresenter.mKeyPressed(EvdevTranslator.switchScanCode(b3));
//            App.mJPresenter.mKeyPressed(EvdevTranslator.switchScanCode(b1));
        } else if (paramDirection == Direction.DIRECTION_UP_RIGHT) {
//            App.mJPresenter.mKeyPressed(EvdevTranslator.switchScanCode(b3));
//            App.mJPresenter.mKeyPressed(EvdevTranslator.switchScanCode(b2));
        } else if (paramDirection == Direction.DIRECTION_DOWN_LEFT) {
//            App.mJPresenter.mKeyPressed(EvdevTranslator.switchScanCode(paramInt));
//            App.mJPresenter.mKeyPressed(EvdevTranslator.switchScanCode(b1));
        } else if (paramDirection == Direction.DIRECTION_DOWN_RIGHT) {
//            App.mJPresenter.mKeyPressed(EvdevTranslator.switchScanCode(paramInt));
//            App.mJPresenter.mKeyPressed(EvdevTranslator.switchScanCode(b2));
        }

        Direction direction = this.mCurrentRockerViewDirection;

        if (direction != null && direction != paramDirection) {
            if (direction == Direction.DIRECTION_UP) {
//                App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b3));
            } else if (this.mCurrentRockerViewDirection == Direction.DIRECTION_DOWN) {
//                App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(paramInt));
            } else if (this.mCurrentRockerViewDirection == Direction.DIRECTION_LEFT) {
//                App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b1));
            } else if (this.mCurrentRockerViewDirection == Direction.DIRECTION_RIGHT) {
//                App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b2));
            } else if (this.mCurrentRockerViewDirection == Direction.DIRECTION_UP_LEFT) {
//                App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b3));
//                App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b1));
            } else if (this.mCurrentRockerViewDirection == Direction.DIRECTION_UP_RIGHT) {
//                App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b3));
//                App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b2));
            } else if (this.mCurrentRockerViewDirection == Direction.DIRECTION_DOWN_LEFT) {
//                App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(paramInt));
//                App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b1));
            } else if (this.mCurrentRockerViewDirection == Direction.DIRECTION_DOWN_RIGHT) {
//                App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(paramInt));
//                App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b2));
            }
        }

        this.mCurrentRockerViewDirection = paramDirection;
    }

    @Override
    public void onRockerViewFinish(int rockerType) {
        LogUtils.i("onRockerViewFinish: " + rockerType);
        this.isOnRock = false;
        byte b1;
        byte b2;
        byte b3;
        int number;
        if (rockerType == 1) {
            b3 = 103;
            number = 108;
            b1 = 105;
            b2 = 106;
        } else {
            b3 = 17;
            number = 31;
            b1 = 30;
            b2 = 32;
        }

        if (this.mCurrentRockerViewDirection == Direction.DIRECTION_UP) {
//            App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b3));
            return;
        }

        if (this.mCurrentRockerViewDirection == Direction.DIRECTION_DOWN) {
//            App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(paramInt));
            return;
        }
        if (this.mCurrentRockerViewDirection == Direction.DIRECTION_LEFT) {
//            App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b1));
            return;
        }
        if (this.mCurrentRockerViewDirection == Direction.DIRECTION_RIGHT) {
//            App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b2));
            return;
        }
        if (this.mCurrentRockerViewDirection == Direction.DIRECTION_UP_LEFT) {
//            App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b3));
//            App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b1));
            return;
        }
        if (this.mCurrentRockerViewDirection == Direction.DIRECTION_UP_RIGHT) {
//            App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b3));
//            App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b2));
            return;
        }
        if (this.mCurrentRockerViewDirection == Direction.DIRECTION_DOWN_LEFT) {
//            App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(paramInt));
//            App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b1));
            return;
        }
        if (this.mCurrentRockerViewDirection == Direction.DIRECTION_DOWN_RIGHT) {
//            App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(paramInt));
//            App.mJPresenter.mKeyReleased(EvdevTranslator.switchScanCode(b2));
        }
    }

    boolean isOnRock = false;

    @Override
    public void onRockerViewStart(int param1Int) {
        this.isOnRock = true;
    }

    @Override
    public void onClick(View v) {
        String keypos_data = ResourceUtils.readAssets2String("key2.json");
        List list = (new Gson()).fromJson(keypos_data, (new TypeToken<List<JVkeyBean>>() {
        }).getType());


        CustomRoundView customRoundView;
        CustomRockerView customRockerView;

        switch (v.getId()) {
            case R.id.tv_vkey_review_preview:
                mCurrentOpMode = 7;
                showPreviewVkey(list);
                break;
            case R.id.tv_exit_preview:
                this.fl_vkey_preview_container.removeView((View) this.mCurrentJVKeyView);
                this.fl_vkey_preview_container.setVisibility(View.GONE);
                if (this.mCurrentOpMode == 5) {
                    this.ll_vkey_review.setVisibility(View.VISIBLE);
                    return;
                }
                this.ll_vkey_choose.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_edit_custom:
                this.mCurrentOpMode = 2;
                this.isCreate = true;
                editConfig(list);
                break;
            case R.id.tv_vkey_review_edit:
                this.mCurrentOpMode = 6;
                isCreate = false;
                editConfig(list);
                break;
            case R.id.btn_new_custom:
                this.mCurrentOpMode = 3;
                showPreviewVkey(list);
                break;
            case R.id.v_custom_vkey_cancel:
//                JiActivity jiActivity2 = this.mJiActivity;
//                if (jiActivity2 != null) {
//                    jiActivity2.mJKeyBoardView.setVisibility(8);
//                    this.mJiActivity.layout_keyboard.setVisibility(8);
//                } else {
                if (this.listener != null) {
                    this.listener.onVisibilityGone();
                }

                changeVKeyConsState(this.v_custom_vkey_cancel);
                this.ll_dialog_cancel_add.setVisibility(View.VISIBLE);
                this.ll_dialog_cancel_add.findViewById(R.id.btn_dialog_cancel_add_cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ll_dialog_cancel_add.setVisibility(View.GONE);
                        v_custom_vkey_cancel.setEnabled(true);
                    }
                });

                this.ll_dialog_cancel_add.findViewById(R.id.btn_dialog_cancel_add_ok).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View param1View) {
                        JiActivityVKeyHelper.this.v_custom_vkey_cancel.setEnabled(true);
                        JiActivityVKeyHelper.this.ll_dialog_cancel_add.setVisibility(View.GONE);
                        JiActivityVKeyHelper.this.ll_vkey_config_cons.setVisibility(View.GONE);
                        JiActivityVKeyHelper.this.fl_custom_vkey_container.removeAllViews();
                        JiActivityVKeyHelper.this.fl_custom_vkey_container.setVisibility(View.GONE);
                        JiActivityVKeyHelper.this.fl_custom_vkey_choose_container.setVisibility(View.GONE);
                        JiActivityVKeyHelper.this.ll_save_config.setVisibility(View.GONE);
                        if (JiActivityVKeyHelper.this.mJiActivity == null) {
                            JiActivityVKeyHelper.this.ll_vkey_choose.setVisibility(View.VISIBLE);
                            return;
                        }
                        if (JiActivityVKeyHelper.this.mJiActivity != null) {
//                            JiActivityVKeyHelper.this.mJiActivity.isCustomVkeyMode = false;
//                            JiActivityVKeyHelper.this.mJiActivity.mJKeyBoardView.isEditMode = false;
//                            JiActivityVKeyHelper.this.mJiActivity.mJKeyBoardView.setVisibility(8);
//                            JiActivityVKeyHelper.this.mJiActivity.layout_keyboard.setVisibility(8);
                        } else if (JiActivityVKeyHelper.this.listener != null) {
                            JiActivityVKeyHelper.this.listener.onVisibilityGone();
//                            Timber.i("jiactivity null 16", new Object[0]);
                        }
                        if (JiActivityVKeyHelper.this.isCreate) {
                            JiActivityVKeyHelper.this.fl_vkey_menu.setVisibility(View.VISIBLE);
                            return;
                        }
                        JiActivityVKeyHelper.this.ll_vkey_choose.setVisibility(View.GONE);
                    }
                });

                break;
            case R.id.v_custom_vkey_mouse:
                changeVKeyConsState(v_custom_vkey_mouse);
                this.fl_custom_vkey_choose_container.setVisibility(View.VISIBLE);
                this.ll_vkey_choose_mouse.setVisibility(View.VISIBLE);
                this.ll_vkey_choose_shake.setVisibility(View.GONE);
                this.ll_vkey_choose_support.setVisibility(View.GONE);

                if (this.mJiActivity != null) {
//                    jiActivity2.mJKeyBoardView.setVisibility(8);
//                    this.mJiActivity.layout_keyboard.setVisibility(8);
                }

                if (this.listener != null) {
                    this.listener.onVisibilityGone();
                }
                break;
            case R.id.v_custom_vkey_key:
                // 全键盘
                changeVKeyConsState(v_custom_vkey_key);

                if (mActivity != null) {
//                    mActivity.isCustomVkeyMode = true;
//                    this.mJiActivity.mJKeyBoardView.isEditMode = true;
//                    this.mJiActivity.mJKeyBoardView.doShiftChange(false);
//                    this.mJiActivity.mJKeyBoardView.setVisibility(0);
//                    this.mJiActivity.mJKeyBoardView.initDown();
//                    this.mJiActivity.layout_keyboard.setVisibility(0);
                }


                if (this.listener != null) {
                    this.listener.onVisibilityVisible();
                }
                this.fl_custom_vkey_choose_container.setVisibility(View.GONE);
                this.ll_vkey_choose_mouse.setVisibility(View.GONE);
                this.ll_vkey_choose_shake.setVisibility(View.GONE);
                this.ll_vkey_choose_support.setVisibility(View.GONE);
                break;
            case R.id.v_custom_vkey_shake:
                changeVKeyConsState(v_custom_vkey_shake);
                this.fl_custom_vkey_choose_container.setVisibility(View.VISIBLE);
                this.ll_vkey_choose_shake.setVisibility(View.VISIBLE);
                this.ll_vkey_choose_mouse.setVisibility(View.GONE);
                this.ll_vkey_choose_support.setVisibility(View.GONE);

                if (this.mJiActivity != null) {
//                    jiActivity2.mJKeyBoardView.setVisibility(8);
//                    this.mJiActivity.layout_keyboard.setVisibility(8);
//                    return;
                }
                if (this.listener != null) {
                    this.listener.onVisibilityGone();
                    return;
                }

                break;
            case R.id.v_custom_vkey_support:
                changeVKeyConsState(v_custom_vkey_support);
                this.fl_custom_vkey_choose_container.setVisibility(View.VISIBLE);
                this.ll_vkey_choose_shake.setVisibility(View.GONE);
                this.ll_vkey_choose_mouse.setVisibility(View.GONE);
                this.ll_vkey_choose_support.setVisibility(View.VISIBLE);
                if (mActivity != null) {
//                    jiActivity2.mJKeyBoardView.setVisibility(8);
//                    this.mJiActivity.layout_keyboard.setVisibility(8);
//                    return;
                }
                if (this.listener != null) {
                    this.listener.onVisibilityGone();
                }
                break;
            case R.id.v_custom_vkey_save:
                break;
            case R.id.iv_custom_vkey_choose_container_close:
                if (mLastClickVKeyConsImageView != null) {
                    mLastClickVKeyConsImageView.setEnabled(true);
                }

                fl_custom_vkey_choose_container.setVisibility(View.GONE);
                break;
            case R.id.ll_custom_vkey_add_mouse_left:
                // 左键
                customRoundView = new CustomRoundView(mActivity);
                customRoundView.setScanCode(-10);
                customRoundView.setBackground(this.mActivity.getResources().getDrawable(R.mipmap.key_mouse_left_normal));
                customRoundView.setNormalBackground(R.mipmap.key_mouse_left_normal);
                customRoundView.setPressBackground(R.mipmap.key_mouse_left_press);
                addCustomRoundButton(customRoundView);
                break;
            case R.id.ll_custom_vkey_add_mouse_right:
                customRoundView = new CustomRoundView(this.mActivity);
                customRoundView.setScanCode(-20);
                customRoundView.setBackground(this.mActivity.getResources().getDrawable(R.mipmap.key_mouse_right_normal));
                customRoundView.setNormalBackground(R.mipmap.key_mouse_right_normal);
                customRoundView.setPressBackground(R.mipmap.key_mouse_right_press);
                addCustomRoundButton(customRoundView);
                break;
            case R.id.ll_custom_vkey_add_mouse_middle:
                customRoundView = new CustomRoundView((Context) this.mActivity);
                customRoundView.setScanCode(-11);
                customRoundView.setBackground(this.mActivity.getResources().getDrawable(R.mipmap.key_mouse_middle_normal));
                customRoundView.setNormalBackground(R.mipmap.key_mouse_middle_normal);
                customRoundView.setPressBackground(R.mipmap.key_mouse_middle_press);
                addCustomRoundButton(customRoundView);
                break;
            case R.id.ll_custom_vkey_add_scroller_up:
                customRoundView = new CustomRoundView((Context) this.mActivity);
                customRoundView.setBackground(this.mActivity.getResources().getDrawable(R.mipmap.scroll_up_normal));
                customRoundView.setNormalBackground(R.mipmap.scroll_up_normal);
                customRoundView.setPressBackground(R.mipmap.scroll_up_press);
                customRoundView.setScanCode(-30);
                addCustomRoundButton(customRoundView);
                break;
            case R.id.ll_custom_vkey_add_scroller_down:
                customRoundView = new CustomRoundView((Context) this.mActivity);
                customRoundView.setBackground(this.mActivity.getResources().getDrawable(R.mipmap.scroll_down_normal));
                customRoundView.setNormalBackground(R.mipmap.scroll_down_normal);
                customRoundView.setPressBackground(R.mipmap.scroll_down_press);
                customRoundView.setScanCode(-31);
                addCustomRoundButton(customRoundView);
                break;
            case R.id.ll_custom_vkey_add_shake_wasd:
                customRockerView = (CustomRockerView) this.mActivity.getLayoutInflater().inflate(R.layout.v_custom_rocker, null);
                customRockerView.vHeight = 15.0F;
                customRockerView.vWidth = 15.0F;
                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                        (int) (customRockerView.vWidth / 100.0D * mActivityWidth),
                        (int) (customRockerView.vHeight / 100.0D * mActivityWidth));

                int leftMargin = this.ll_vkey_config_cons.getLeft();
                int radius = this.ll_vkey_config_cons.getWidth() / 2;
                int topMargin = this.ll_vkey_config_cons.getTop();
                int height = this.ll_vkey_config_cons.getHeight();
                layoutParams.leftMargin = leftMargin + radius - ConvertUtils.dp2px(100.0f) / 2;
                layoutParams.topMargin = topMargin + height + 20;

                customRockerView.setLayoutParams(layoutParams);
                customRockerView.setOnCusKeyViewClickListener(this);
                customRockerView.setKeyType(2);
                customRockerView.setRockType(0);
                customRockerView.setRockBackOut(
                        ((BitmapDrawable) this.mActivity.getResources().getDrawable(R.mipmap.rock_wasd_normal)).getBitmap());
                customRockerView.setRockBackIn(
                        ((BitmapDrawable) this.mActivity.getResources().getDrawable(R.mipmap.rock_bg_in)).getBitmap());
                customRockerView.setBackground(this.mActivity.getResources().getDrawable(R.mipmap.rock_wasd_press));
                this.fl_custom_vkey_container.addView((View) customRockerView);
                break;
            case R.id.ll_custom_vkey_add_shake_arrow:
                customRockerView = (CustomRockerView) this.mActivity.getLayoutInflater().inflate(R.layout.v_custom_rocker, null);
                customRockerView.vHeight = 15.0F;
                customRockerView.vWidth = 15.0F;
                FrameLayout.LayoutParams layoutParams1 = new FrameLayout.LayoutParams(
                        (int) (customRockerView.vWidth / 100.0D * mActivityWidth),
                        (int) (customRockerView.vHeight / 100.0D * mActivityWidth));

                leftMargin = this.ll_vkey_config_cons.getLeft();
                radius = this.ll_vkey_config_cons.getWidth() / 2;
                topMargin = this.ll_vkey_config_cons.getTop();
                height = this.ll_vkey_config_cons.getHeight();
                layoutParams1.leftMargin = leftMargin + radius - ConvertUtils.dp2px(100.0f) / 2;
                layoutParams1.topMargin = topMargin + height + 20;

                customRockerView.setLayoutParams((ViewGroup.LayoutParams) layoutParams1);
                customRockerView.setOnCusKeyViewClickListener(this);
                customRockerView.setKeyType(2);
                customRockerView.setRockType(1);
                customRockerView.setRockBackOut(
                        ((BitmapDrawable) this.mActivity.getResources().getDrawable(R.mipmap.rock_bg_out_normal)).getBitmap());
                customRockerView.setRockBackIn(
                        ((BitmapDrawable) this.mActivity.getResources().getDrawable(R.mipmap.rock_bg_in)).getBitmap());
                customRockerView.setBackground(this.mActivity.getResources().getDrawable(R.mipmap.rock_bg_out_press));
                this.fl_custom_vkey_container.addView((View) customRockerView);
                break;
            case R.id.ll_custom_vkey_add_fire:
                customRoundView = new CustomRoundView((Context) this.mActivity);
                customRoundView.setScanCode(-10);
                customRoundView.setBackground(this.mActivity.getResources().getDrawable(R.mipmap.key_fire_normal));
                customRoundView.setNormalBackground(R.mipmap.key_fire_normal);
                customRoundView.setPressBackground(R.mipmap.key_fire_press);
                customRoundView.setFling(true);
                addCustomRoundButton(customRoundView);
                break;

            case R.id.ll_custom_vkey_touch_lr_sw:
                customRoundView = new CustomRoundView(this.mActivity);
                customRoundView.setBackground(this.mActivity.getResources().getDrawable(R.mipmap.touch_mode_change_normal));
                customRoundView.setNormalBackground(R.mipmap.touch_mode_change_normal);
                customRoundView.setPressBackground(R.mipmap.touch_mode_change_press);
                customRoundView.setScanCode(-40);
                customRoundView.setLongPress(true);
                addCustomRoundButton(customRoundView);
                break;
            case R.id.ll_custom_vkey_mouse_lr_sw:
                customRoundView = new CustomRoundView(this.mActivity);
                customRoundView.setScanCode(-41);
                customRoundView.setBackground(this.mActivity.getResources().getDrawable(R.mipmap.mouse_lr_sw));
                customRoundView.setNormalBackground(R.mipmap.mouse_lr_sw);
                customRoundView.setPressBackground(R.mipmap.mouse_lr_sw_press);
                customRoundView.setLongPress(true);
                addCustomRoundButton(customRoundView);
                break;
            case R.id.ll_custom_vkey_add_support_horizontal:
                for (int i = 0; i < fl_custom_vkey_container.getChildCount(); i++) {
                    View view = fl_custom_vkey_container.getChildAt(i);
                    if (view instanceof SupportLineView && ((SupportLineView) view).getOrientation() == 2) {
                        return;
                    }
                }

                layoutParams = new FrameLayout.LayoutParams(-1, 60);
                layoutParams.topMargin = App.SCREEN_WIDTH / 2;
                SupportLineView supportLineView = new SupportLineView((Context) this.mActivity);
                supportLineView.setLayoutParams((ViewGroup.LayoutParams) layoutParams);
                this.fl_custom_vkey_container.addView((View) supportLineView);
                supportLineView.setOrientation(2);
                supportLineView.setOnSupportLineViewClickListener(this);

                break;
            case R.id.ll_custom_vkey_add_support_vertical:
                for (int i = 0; i < fl_custom_vkey_container.getChildCount(); i++) {
                    View view = fl_custom_vkey_container.getChildAt(i);
                    if (view instanceof SupportLineView && ((SupportLineView) view).getOrientation() == 1) {
                        return;
                    }
                }

                layoutParams = new FrameLayout.LayoutParams(60, -1);
                layoutParams.leftMargin = App.SCREEN_HEIGHT / 2;
                supportLineView = new SupportLineView(this.mActivity);
                supportLineView.setLayoutParams(layoutParams);
                this.fl_custom_vkey_container.addView(supportLineView);
                supportLineView.setOrientation(1);
                supportLineView.setOnSupportLineViewClickListener(this);
                break;
            case R.id.ll_custom_vkey_lock_screen_slide:
                customRoundView = new CustomRoundView((Context) this.mActivity);
                customRoundView.setScanCode(-42);
                customRoundView.setBackground(this.mActivity.getResources().getDrawable(R.mipmap.lock_screen_slide));
                customRoundView.setNormalBackground(R.mipmap.lock_screen_slide);
                customRoundView.setPressBackground(R.mipmap.lock_screen_slide_press);
                customRoundView.setLongPress(true);
                addCustomRoundButton(customRoundView);
                break;

            default:
                break;
        }

    }

    private View mLastClickVKeyConsImageView;

    private void changeVKeyConsState(View v_custom_vkey_cancel) {
        if (mLastClickVKeyConsImageView != null) {
            mLastClickVKeyConsImageView.setEnabled(true);
        }
        mLastClickVKeyConsImageView = v_custom_vkey_cancel;
        v_custom_vkey_cancel.setEnabled(false);
    }

    boolean isCreate = false;

    private void editConfig(List<JVkeyBean> paramList) {
        LogUtils.i("paramList: " + paramList.size());

        this.ll_vkey_choose.setVisibility(View.GONE);
        this.ll_vkey_cons.setVisibility(View.GONE);
        this.fl_vkey_menu.setVisibility(View.GONE);

        clearKey();

        this.fl_vkey_custom.setVisibility(View.VISIBLE);
        this.fl_custom_vkey_container.setVisibility(View.VISIBLE);
        this.ll_vkey_config_cons.setVisibility(View.VISIBLE);

        if (mCurrentOpMode == 6) {
            this.ll_vkey_review.setVisibility(View.GONE);
        }

        for (JVkeyBean jVkeyBean : paramList) {
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(100, 100);
            layoutParams.leftMargin = (int) (jVkeyBean.getLeftMargin() / 100.0D * mActivityWidth);
            layoutParams.topMargin = (int) (jVkeyBean.getTopMargin() / 100.0D * mActivityHeight);
            layoutParams.width = (int) (jVkeyBean.getWidth() / 100.0D * mActivityWidth);
            layoutParams.height = (int) (jVkeyBean.getHeight() / 100.0D * mActivityWidth);
            boolean bool;
            if (this.fl_custom_vkey_container.getVisibility() == View.VISIBLE) {
                bool = true;
            } else {
                bool = false;
            }

            if (jVkeyBean.getKeyType() == 1) {
                CustomRoundView customRoundView = new CustomRoundView(this.mActivity);
                customRoundView.setFling(jVkeyBean.isFling());
                customRoundView.setSize(jVkeyBean.getSize());
                customRoundView.vHeight = jVkeyBean.getHeight();
                customRoundView.vWidth = jVkeyBean.getWidth();
                customRoundView.setScanCode(jVkeyBean.getScanCode());
                customRoundView.setRound(jVkeyBean.isRound());

                if (jVkeyBean.isRound()) {
                    customRoundView.setBackground(this.mActivity.getResources().getDrawable(CustomVKeyUtil.getVKeyBackground(customRoundView.getScanCode(), customRoundView.isFling())));
                    customRoundView.setNormalBackground(CustomVKeyUtil.getVKeyBackground(customRoundView.getScanCode(), customRoundView.isFling()));
                    customRoundView.setPressBackground(CustomVKeyUtil.getVKeyPressBackground(customRoundView.getScanCode(), customRoundView.isFling()));
                } else {
                    customRoundView.setBackground(this.mActivity.getResources().getDrawable(R.mipmap.key_space_normal));
                    customRoundView.setNormalBackground(R.mipmap.key_space_normal);
                    customRoundView.setPressBackground(R.mipmap.key_space_press);
                }
                customRoundView.setGravity(Gravity.CENTER);
                customRoundView.setTypeface(Typeface.DEFAULT_BOLD);
                customRoundView.setTextColor(ContextCompat.getColor(mActivity, R.color.white));
                customRoundView.setKeyType(1);
                customRoundView.setLongPress(jVkeyBean.isLongPress());
                customRoundView.setLayoutParams(layoutParams);
                customRoundView.setContinuous(jVkeyBean.isContinuous());
                customRoundView.setContinuousFrequency(jVkeyBean.getContinuousFrequency());
                customRoundView.setOnCusKeyViewClickListener(this);
                this.fl_custom_vkey_container.addView((View) customRoundView);
                continue;
            }

            CustomRockerView customRockerView = (CustomRockerView) this.mActivity.getLayoutInflater().inflate(R.layout.v_custom_rocker, null);
            customRockerView.setKeyType(2);
            customRockerView.setRockType(jVkeyBean.getRockType());
            customRockerView.setOnCusKeyViewClickListener(this);
            customRockerView.setLayoutParams((ViewGroup.LayoutParams) layoutParams);
            customRockerView.setSize(jVkeyBean.getSize());
            customRockerView.setRockerRadiusBySize(jVkeyBean.getSize());
            customRockerView.vHeight = jVkeyBean.getHeight();
            customRockerView.vWidth = jVkeyBean.getWidth();
            if (jVkeyBean.getRockType() == 0) {
                customRockerView.setRockBackOut(((BitmapDrawable) this.mActivity.getResources().getDrawable(R.mipmap.rock_wasd_normal)).getBitmap());
                customRockerView.setRockBackIn(((BitmapDrawable) this.mActivity.getResources().getDrawable(R.mipmap.rock_bg_in)).getBitmap());
                customRockerView.setBackground(this.mActivity.getResources().getDrawable(R.mipmap.rock_wasd_press));
            } else {
                customRockerView.setRockBackOut(((BitmapDrawable) this.mActivity.getResources().getDrawable(R.mipmap.rock_bg_out_normal)).getBitmap());
                customRockerView.setRockBackIn(((BitmapDrawable) this.mActivity.getResources().getDrawable(R.mipmap.rock_bg_in)).getBitmap());
                customRockerView.setBackground(this.mActivity.getResources().getDrawable(R.mipmap.rock_bg_out_press));
            }
            this.fl_custom_vkey_container.addView((View) customRockerView);
        }
    }

    private int                      settingSize;
    private FrameLayout.LayoutParams layoutParams;

    private int sizIn5Height;
    private int sizeIn5Width;

    public View dialog;


    @Override
    public void onCusKeyViewClick(final CustomKeyView view) {
        LogUtils.i("onCusKeyViewClick");

        this.settingSize = view.getSize();
        this.layoutParams = new FrameLayout.LayoutParams((FrameLayout.LayoutParams) view.getLayoutParams());

        if (view instanceof CustomRockerView) {
            this.sizeIn5Width = 15;
            this.sizIn5Height = 15;
            this.dialog.findViewById(R.id.rocker).setVisibility(View.VISIBLE);
            this.dialog.findViewById(R.id.rb_key).setVisibility(View.INVISIBLE);
        } else {
            RoundView roundView = (RoundView) this.dialog.findViewById(R.id.rb_key);
            this.sizeIn5Width = 8;
            this.sizIn5Height = 8;
            roundView.setText(view.getText());
            roundView.setGravity(Gravity.CENTER);
            roundView.setBackgroundResource(CustomVKeyUtil.getVKeyBackground(view.getScanCode(), view.isFling()));
            this.dialog.findViewById(R.id.rb_key).setVisibility(View.VISIBLE);
            this.dialog.findViewById(R.id.rocker).setVisibility(View.INVISIBLE);
            if (view.getText().toString().equalsIgnoreCase("space")) {
                this.sizeIn5Width = 20;
                this.sizIn5Height = 5;
            }
        }

        final int height = (int) (sizIn5Height / 100.0D * mActivityWidth);
        final int width = (int) (sizeIn5Width / 100.0D * mActivityWidth);

        LogUtils.i("sizIn5Height: " + sizIn5Height + " sizeIn5Width: " + sizeIn5Width + " height: " + height + " width: " + width);

        this.isCusKeyLongPress = view.isLongPress();
        this.isCusKeyContinuityPress = view.isContinuous();
        if (view.getScanCode() == -40 || view.getScanCode() == -41 || view.getScanCode() == -42) {
            this.tv_once.setVisibility(View.GONE);
        } else {
            this.tv_once.setVisibility(View.VISIBLE);
        }

        this.continuityInterval = view.getContinuousFrequency();
        tv_frequency.setText(String.valueOf(continuityInterval));

        if (view.getScanCode() == -40 || view.getScanCode() == -41 || view.getScanCode() == -42 || view.getScanCode() == 0
                || view.isFling()) {
            this.tv_continuity.setVisibility(View.GONE);
        } else {
            this.tv_continuity.setVisibility(View.VISIBLE);
        }

        if (this.isCusKeyContinuityPress) {
            this.layout_frequency.setVisibility(View.VISIBLE);
        } else {
            this.layout_frequency.setVisibility(View.INVISIBLE);
        }

        if (this.isCusKeyContinuityPress) {
            this.tv_continuity.setTextColor(Color.parseColor("#86FF02"));
            this.tv_long.setTextColor(ContextCompat.getColor(mActivity, R.color.white));
            this.tv_once.setTextColor(ContextCompat.getColor(mActivity, R.color.white));
        } else if (this.isCusKeyLongPress) {
            this.tv_long.setTextColor(Color.parseColor("#86FF02"));
            this.tv_once.setTextColor(ContextCompat.getColor(mActivity, R.color.white));
            this.tv_continuity.setTextColor(ContextCompat.getColor(mActivity, R.color.white));
        } else {
            this.tv_once.setTextColor(Color.parseColor("#86FF02"));
            this.tv_long.setTextColor(ContextCompat.getColor(mActivity, R.color.white));
            this.tv_continuity.setTextColor(ContextCompat.getColor(mActivity, R.color.white));
        }

        tv_size.setText(String.valueOf(settingSize));
        this.iv_btn_left_size.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (settingSize > 2) {
                    settingSize -= 2;
                    setKeySize(view, width, height);
                }
            }
        });
        this.iv_btn_right_size.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View param1View) {
                if (JiActivityVKeyHelper.this.settingSize < 10) {
                    settingSize += 2;
                    setKeySize(view, width, height);
                }
            }
        });

        this.iv_btn_right_frequency.setOnTouchListener(this);
        this.iv_btn_left_frequency.setOnTouchListener(this);

        this.dialog.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View param1View) {
                CustomKeyView customKeyView = view;
                if (customKeyView instanceof CustomRockerView) {
                    ((CustomRockerView) customKeyView).setRockerRadiusBySize(customKeyView.getSize());
                }
                view.setLayoutParams(JiActivityVKeyHelper.this.layoutParams);
                JiActivityVKeyHelper.this.dialog.setVisibility(View.GONE);
            }
        });

        this.dialog.findViewById(R.id.tv_remove).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View param1View) {
                JiActivityVKeyHelper.this.fl_custom_vkey_container.removeView(view);
                JiActivityVKeyHelper.this.dialog.setVisibility(View.GONE);
            }
        });

        this.tv_once.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View param1View) {
//                JiActivityVKeyHelper.access$602(JiActivityVKeyHelper.this, false);
//                JiActivityVKeyHelper.access$702(JiActivityVKeyHelper.this, false);

                isCusKeyLongPress = false;
                isCusKeyContinuityPress = false;

                JiActivityVKeyHelper.this.tv_once.setTextColor(Color.parseColor("#86FF02"));
                JiActivityVKeyHelper.this.tv_long.setTextColor(JiActivityVKeyHelper.this.mActivity.getResources().getColor(R.color.white));
                JiActivityVKeyHelper.this.tv_continuity.setTextColor(JiActivityVKeyHelper.this.mActivity.getResources().getColor(R.color.white));
                JiActivityVKeyHelper.this.layout_frequency.setVisibility(View.INVISIBLE);
            }
        });
        this.tv_long.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View param1View) {

                isCusKeyLongPress = true;
                isCusKeyContinuityPress = false;

//                JiActivityVKeyHelper.access$602(JiActivityVKeyHelper.this, true);
//                JiActivityVKeyHelper.access$702(JiActivityVKeyHelper.this, false);
                JiActivityVKeyHelper.this.tv_long.setTextColor(Color.parseColor("#86FF02"));
                JiActivityVKeyHelper.this.tv_once.setTextColor(JiActivityVKeyHelper.this.mActivity.getResources().getColor(R.color.white));
                JiActivityVKeyHelper.this.tv_continuity.setTextColor(JiActivityVKeyHelper.this.mActivity.getResources().getColor(R.color.white));
                JiActivityVKeyHelper.this.layout_frequency.setVisibility(View.INVISIBLE);
            }
        });
        this.tv_continuity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View param1View) {
                isCusKeyContinuityPress = true;
                isCusKeyLongPress = false;
//                JiActivityVKeyHelper.access$602(JiActivityVKeyHelper.this, false);
//                JiActivityVKeyHelper.access$702(JiActivityVKeyHelper.this, true);
                JiActivityVKeyHelper.this.tv_continuity.setTextColor(Color.parseColor("#86FF02"));
                JiActivityVKeyHelper.this.tv_long.setTextColor(JiActivityVKeyHelper.this.mActivity.getResources().getColor(R.color.white));
                JiActivityVKeyHelper.this.tv_once.setTextColor(JiActivityVKeyHelper.this.mActivity.getResources().getColor(R.color.white));
                JiActivityVKeyHelper.this.layout_frequency.setVisibility(View.VISIBLE);
            }
        });
        this.dialog.findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View param1View) {
                view.setLongPress(JiActivityVKeyHelper.this.isCusKeyLongPress);
                view.setSize(JiActivityVKeyHelper.this.settingSize);
                view.setContinuous(JiActivityVKeyHelper.this.isCusKeyContinuityPress);
                view.setContinuousFrequency(JiActivityVKeyHelper.this.continuityInterval);
                view.setContinuousFrequency(JiActivityVKeyHelper.this.continuityInterval);
                JiActivityVKeyHelper.this.dialog.setVisibility(View.GONE);
            }
        });

        this.dialog.setVisibility(View.VISIBLE);

    }

    float   continuityInterval      = 0.0f;
    boolean isCusKeyLongPress       = false;
    boolean isCusKeyContinuityPress = false;
    private Handler mHandler = new Handler();


    @Override
    public boolean onTouch(final View v, MotionEvent event) {
        Log.i(TAG, "onTouch");

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    if (v == iv_btn_right_frequency) {
                        enlarge();
                    } else if (v == iv_btn_left_frequency) {
                        reduce();
                    }
                }
            };
            this.mHandler.postDelayed(runnable, 10L);
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            Log.i(TAG, "onTouch--- 抬起");
            this.mHandler.removeCallbacksAndMessages(null);
        }

        return true;
    }

    private void enlarge() {
        float f = this.continuityInterval;
        if (f < 3.0F) {
            this.continuityInterval = getDecimalFormat(f, 0.1F);
            TextView textView = this.tv_frequency;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(this.continuityInterval);
            stringBuilder.append("");
            textView.setText(stringBuilder.toString());
        }
    }

    private void reduce() {
        float f = this.continuityInterval;
        if (f > 0.1D) {
            this.continuityInterval = getDecimalFormat(f, -0.1F);
            TextView textView = this.tv_frequency;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(this.continuityInterval);
            stringBuilder.append("");
            textView.setText(stringBuilder.toString());
        }
    }

    public float getDecimalFormat(float paramFloat1, float paramFloat2) {
        paramFloat2 = Float.parseFloat((new DecimalFormat("0.0")).format((paramFloat2 + paramFloat1)));
        return (paramFloat2 <= 3.0F && paramFloat2 >= 0.1D) ? paramFloat2 : paramFloat1;
    }

    private void setKeySize(CustomKeyView paramCustomKeyView, int width, int height) {
        TextView textView = this.tv_size;
        textView.setText(String.valueOf(settingSize));

        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) paramCustomKeyView.getLayoutParams();

        if (settingSize < 5) {
            layoutParams.width = (int) (width - (0.5f - settingSize / 10.0f) * width);
            layoutParams.height = (int) (height - (0.5f - settingSize / 10.0f) * height);
        } else {

            layoutParams.width = (int) (width + (settingSize / 10.0f - 0.5f) * width);
            layoutParams.height = (int) (height + (settingSize / 10.0f - 0.5f) * height);
        }

        if (paramCustomKeyView instanceof CustomRockerView) {
            ((CustomRockerView) paramCustomKeyView).setRockerRadiusBySize(this.settingSize);
        }
        paramCustomKeyView.setLayoutParams(layoutParams);
    }

    private KeyBoardListener listener;

    public void setKeyBoardListener(KeyBoardListener paramKeyBoardListener) {
        this.listener = paramKeyBoardListener;
    }

    @Override
    public void onSupportLineViewClick(final SupportLineView supportLineView) {
        final View view = this.mActivity.getLayoutInflater().inflate(R.layout.dialog_layout_supportline, null);
        final Dialog dialog = DialogFactory.showCustomDialog(this.mActivity, view);
        if (supportLineView.getOrientation() == 1) {
            ((ImageView) view.findViewById(R.id.iv_img)).setImageResource(R.mipmap.support_1);
        } else {
            ((ImageView) view.findViewById(R.id.iv_img)).setImageResource(R.mipmap.support_2);
        }
        view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View param1View) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.tv_remove).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View param1View) {
                JiActivityVKeyHelper.this.fl_custom_vkey_container.removeView(supportLineView);
                dialog.dismiss();
            }
        });
        if (mActivity != null) {
//            jiActivity.mJKeyBoardView.setVisibility(8);
//            this.mJiActivity.layout_keyboard.setVisibility(8);
        } else {
            KeyBoardListener keyBoardListener = this.listener;
            if (keyBoardListener != null)
                keyBoardListener.onVisibilityGone();
        }
        dialog.show();
    }

    public static interface KeyBoardListener {
        void onImportKeyBgImage();

        void onVisibilityGone();

        void onVisibilityVisible();
    }

    public static interface KeypadCommListener {
        void onHide();
    }

    public void addCustomRoundButton(CustomRoundView customRoundView) {
        customRoundView.vWidth = 8.0F;
        customRoundView.vHeight = 8.0F;

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                (int) (customRoundView.vWidth / 100.0D * mActivityWidth),
                (int) (customRoundView.vHeight / 100.0D * mActivityWidth));

        int leftMargin = this.ll_vkey_config_cons.getLeft();
        int radius = this.ll_vkey_config_cons.getWidth() / 2;
        int topMargin = this.ll_vkey_config_cons.getTop();
        int height = this.ll_vkey_config_cons.getHeight();

        layoutParams.leftMargin = leftMargin + radius - 75;
        layoutParams.topMargin = topMargin + height + 20;

        customRoundView.setTextColor(this.mActivity.getResources().getColor(R.color.white));
        customRoundView.setOnCusKeyViewClickListener(this);
        customRoundView.setKeyType(1);
        customRoundView.setLayoutParams(layoutParams);
        this.fl_custom_vkey_container.addView(customRoundView);
    }
}
